﻿package com.icebergteam.warfacewidget.core;

import java.util.ArrayList;
import java.util.Locale;

import android.content.res.Configuration;

import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.settings.GeneralSettings;
import com.icebergteam.warfacewidget.R;

public class LocaleManager {
	// Regions
	public final int REGION_RUSSIA				= 00;
//	public final int REGION_EUROPE				= 01;
//	public final int REGION_NORTH_AMERICA		= 02;
//	public final int REGION_SOUTHEAST_ASIA		= 03;
//	public final int REGION_REPUBLIC_OF_KOREA	= 04;
	public final int REGION_COUNT				= 01;
	
	public final static String className = LocaleManager.class.getCanonicalName();
	
	private final String regions[] = { 
			".ru",
//			".eu",
//			".com",
//			"-sea.com",
//			".kr" 
			};
	
	private String langs[][] = { 
									{ "ru", "uk" }, 
//									{ "en", "es", "de", "fr", "cs", "pl", "tr" },
//									{ "en", "us" }, 
//									{ "en", "th" }, 
//									{ "ko" }
								};
	private final String supportedLangs[] = {
//			"en",
//			"es",
//			"de",
//			"fr",
//			"cs",
//			"pl",
//			"tr",
			"ru",
//			"th",
//			"ko"
			};
	
	private final int UI_LANG_ID	= 0;
	private final int UI_LANG_VALUE	= 1;
	private final String supportedUILangs[][] = { 
													{/*"en",	*/	"ru",		"uk"},
													{/*"English",	*/"Русский",	"Українська"},
												};
	
	public LocaleManager() throws Exception {
	
	}
	// Server
	private boolean isEnabledForRegion(int regionId, String lang)	{
		if(regionId < 0 || regionId > REGION_COUNT)
			return false;
		for (String currString : langs[regionId]) {
			if(lang.equals(currString))
				return true;
		}
		return false;
	}
	
	public void setupServerLocaleSettings() throws Exception	{
		String country	= Locale.getDefault().getCountry();
		String lang		= Locale.getDefault().getLanguage();
		
		GeneralSettings settingsInstance = AppManager.getGeneralSettings();
//		if(isEnabledForRegion(REGION_RUSSIA, lang))	{
//			lang = isLangSupported(lang) ? lang : langs[REGION_RUSSIA][0];
//			settingsInstance.setServerRegion(regions[REGION_RUSSIA]);
//			settingsInstance.setServerLang(lang);
//		}
//		else if(isEnabledForRegion(REGION_NORTH_AMERICA, lang) && country.equals("US"))	{
//			lang = isLangSupported(lang) ? lang : langs[REGION_NORTH_AMERICA][0];
//			settingsInstance.setServerRegion(regions[REGION_NORTH_AMERICA]);
//			settingsInstance.setServerLang(lang);
//		}
//		else if(isEnabledForRegion(REGION_EUROPE, lang))	{
//			lang = isLangSupported(lang) ? lang : langs[REGION_EUROPE][0];
//			settingsInstance.setServerRegion(regions[REGION_EUROPE]);
//			settingsInstance.setServerLang(lang);
//		}
//		else if(isEnabledForRegion(REGION_SOUTHEAST_ASIA, lang))	{
//			lang = isLangSupported(lang) ? lang : langs[REGION_SOUTHEAST_ASIA][0];
//			settingsInstance.setServerRegion(regions[REGION_SOUTHEAST_ASIA]);
//			settingsInstance.setServerLang(lang);
//		}
//		else if(isEnabledForRegion(REGION_REPUBLIC_OF_KOREA, lang))	{
//			lang = isLangSupported(lang) ? lang : langs[REGION_REPUBLIC_OF_KOREA][0];
//			settingsInstance.setServerRegion(regions[REGION_REPUBLIC_OF_KOREA]);
//			settingsInstance.setServerLang(lang);
//		}
//		else	{
//			lang = langs[REGION_NORTH_AMERICA][0];
//			settingsInstance.setServerRegion(regions[REGION_NORTH_AMERICA]);
//			settingsInstance.setServerLang(lang);
//		}
//		LogManager.LogV(LocaleManager.className, LogManager.getExecuteMethodName(), settingsInstance.getUILang());
		
		lang = langs[REGION_RUSSIA][0];
		settingsInstance.setServerRegion(regions[REGION_RUSSIA]);
		settingsInstance.setServerLang(lang);
		
	}
	
	private boolean isLangSupported(String id)	{
		for (String current : supportedLangs) {
			if(current.equals(id))
				return true;
		}
		return false;		
	}
// Region operations	
	public String[] getRegions()	{
		return regions;
	}
	
	public int getRegionIndex(String region) throws Exception	{
		for(int i = 0; i < regions.length; i++)	{
			if(regions[i].equals(region))
				return i;
		}
		throw new Exception(LogManager.getExecuteMethodName() + " - ERROR: Wrong region");
	}
	
	public String getRegionByIndex(int index)	{
		return regions[index];
	}
	
	public int getCurrentRegionIndex() throws Exception	{
		return getRegionIndex(AppManager.getGeneralSettings().getServerRegion());
	}
	
	public String getCurrentRegion() throws Exception	{
		return AppManager.getGeneralSettings().getServerRegion();
	}
	
	public String[] getRegionLangs(String region) throws Exception	{
		for (int i = 0; i < regions.length; i++) {
			if(regions[i].equals(region))
				return langs[i];
		}
		throw new Exception(LogManager.getExecuteMethodName() + " - ERROR: Wrong languge id");
	}
	
	public String[] getCurrentRegionLangs() throws Exception	{
		String[] regionLangs = getRegionLangs(AppManager.getGeneralSettings().getServerRegion());
		// Remove unsupported languages
		ArrayList<String> filtredLangsList = new ArrayList<String>();
		for (String current : regionLangs) {
			if(isLangSupported(current))
				 filtredLangsList.add(current);
		}
		return (String[]) filtredLangsList.toArray(new String[filtredLangsList.size()]);
	}
	
	public String getCurrentServerLang() throws Exception	{
		return AppManager.getGeneralSettings().getServerLang();
	}
	
	public int getCurrentServerLangIndex() throws Exception	{
		String[] langList = getCurrentRegionLangs();
		for(int i = 0; i < langList.length; i++)	
			if(langList[i].equals(AppManager.getGeneralSettings().getServerLang()))
				return i;
		throw new Exception(LogManager.getExecuteMethodName()+ " ERROR: Wrong languge id");
	}
	// UI
	public String[] getSupportedUILangsIds()	{
		return supportedUILangs[UI_LANG_ID];
	}
	
	public String[] getSupportedUILangs()	{
		return supportedUILangs[UI_LANG_VALUE];
	}
	
	public int getUILangIndex(String lang) throws Exception	{
		for(int i = 0; i < supportedUILangs[UI_LANG_ID].length; i++)	{
			if(supportedUILangs[UI_LANG_ID][i].equals(lang) || supportedUILangs[UI_LANG_VALUE][i].equals(lang))	{
				return i;
			}
		}
		throw new Exception(LogManager.getExecuteMethodName() + " ERROR: Wrong languge id");
	}
// Current language operations
	public int getCurrentUILangIndex() throws Exception	{
		return getUILangIndex(AppManager.getGeneralSettings().getUILang());
	}
	
	public String getCurrentUILang() throws Exception	{
		return supportedUILangs[UI_LANG_VALUE][getCurrentUILangIndex()];
	}
	
	public String getCurrentUILangId() throws Exception	{
		return supportedUILangs[UI_LANG_ID][getCurrentUILangIndex()];
	}
// Supported languages	
	public String getSuppotedUILang() {
		String deviceLang = Locale.getDefault().getLanguage();
		for (String current : supportedUILangs[UI_LANG_ID]) {
			if(current.equals(deviceLang))	{
				return deviceLang;
			}
		}
		return supportedUILangs[UI_LANG_ID][0];
	}
	
	public void updateLocale() throws Exception	{
		Locale locale;
		locale = new Locale(AppManager.getGeneralSettings().getUILang());
		Configuration config = new Configuration();
		config.locale = locale;
		AppManager.getContext().getResources().updateConfiguration(config, 
		AppManager.getContext().getResources().getDisplayMetrics());
	}
// Localized hours
	public String getLocalizedHours(int hours) throws Exception	{
		int ending = hours % 10;
		// Check EN
		if(getCurrentUILangId().equals("en"))	{
			if(hours != 1)
				return AppManager.getContext().getString(R.string.localized_hours_en_plural);
			else
				return AppManager.getContext().getString(R.string.localized_hours_en_singular);
		}
		// Check for 11..19 hours
		if((hours > 10 && hours < 20))	{
			if(getCurrentUILangId().equals("uk"))
				return AppManager.getContext().getString(R.string.localized_hours_uk_plural_567890);
			else if(getCurrentUILangId().equals("ru"))
				return AppManager.getContext().getString(R.string.localized_hours_ru_plural_567890);
		}
		// check for 1
		if(ending == 1)	{
			if(getCurrentUILangId().equals("uk"))
				return AppManager.getContext().getString(R.string.localized_hours_uk_singular);
			else if(getCurrentUILangId().equals("ru"))
				return AppManager.getContext().getString(R.string.localized_hours_ru_singular);
		}
		// check 567890
		if(ending < 2 || ending > 4)	{
			if(getCurrentUILangId().equals("uk"))
				return AppManager.getContext().getString(R.string.localized_hours_uk_plural_567890);
			else if(getCurrentUILangId().equals("ru"))
				return AppManager.getContext().getString(R.string.localized_hours_ru_plural_567890);
		}
		// check 234
		else	{
			if(getCurrentUILangId().equals("uk"))
				return AppManager.getContext().getString(R.string.localized_hours_uk_plural_234);
			else if(getCurrentUILangId().equals("ru"))
				return AppManager.getContext().getString(R.string.localized_hours_ru_plural_234);
		}
		
		return "";
	}
}
